#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>

//因为ai <= 1e9 又因为 2的30次方为1073741824远大于1e9， 此时金币的数量为0，因此使用坏钥匙开的数量因小于30
//状态表示 a[i][j]打开第i个箱子使用j把坏钥匙的最大值
// a[i][j] = max(a[i - 1][j] + p[i] / power[j] - k, a[i - 1][j - 1] + p[i] / power[j])
using namespace std;
using LL = long long;

const int N = 1e5 + 10;
const LL LLF = 2147483647;

int n, k;
int p[N];
LL dp[N][40];
LL power[40];

LL solve(){
    cin >> n >> k;
    LL maxv = -LLF;

    for (int i = 1; i <= n; i ++){
        for (int j = 0; j <= 30; j ++){
            dp[i][j] = -LLF;
        }
    }

    for (int i = 1; i <= n; i ++){
        cin >> p[i];
    }

    for (int i = 1; i <= n; i ++){
        for (int j = 0; j <= 30; j ++){
            if(j == 0){
                dp[i][j] = dp[i - 1][j] + p[i] / power[j] - k;
            }else{
                dp[i][j] = max(dp[i - 1][j] + p[i] / power[j] - k, dp[i - 1][j - 1] + p[i] / power[j]);
            }

            maxv = max(maxv, dp[i][j]);
        }
    }

    return maxv;
}

int main()
{
    
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);


    int T;
    cin >> T;

    power[0] = 1;
    for (int i = 1; i <= 30; i ++){
        power[i] = power[i - 1] * 2;
    }

    for (int i = 0; i < T; i++)
    {
        printf("%lld\n", solve());
    }
}